package com.coollang.smartbra.presenter2;

import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.interfaces.SportPlanView;
import com.coollang.smartbra.model.SportPlanModel;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.Urls;
import com.google.gson.Gson;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: Timmy
 * @data: 2018/1/17
 * @Fuction:
 */

public class NewSportPlanActivityPresenter extends AbstractMvpPresenter<SportPlanView> {

    private int mTodayPosition;
    private List<SportPlanModel.DataBean> mDataBeen;


    public NewSportPlanActivityPresenter() {
    }

    /**
     * 获取数据
     */
    public void initData() {
        getMyPlan();
    }

    private void getMyPlan() {
        MyHttp.getInstance().getMyPlan(new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                SportPlanModel model = new Gson().fromJson(results, SportPlanModel.class);
                if (model.getCode() == Urls.CODE_SUCCESS) {
                    mDataBeen = model.getData();
                    int size = mDataBeen.size();

                    for (int i = 0; i < size; i++) {
                        SportPlanModel.DataBean dataBean = mDataBeen.get(i);
                        String date = dataBean.getDate();
                        boolean today = TimeUtils.isToday(date);
                        if (today) {
                            mTodayPosition = i;
                            break;
                        }
                    }
                    getmMvpView().setPlanData(mDataBeen);
                } else {
                    getmMvpView().setPlanData(null);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                e.printStackTrace();

                getmMvpView().setPlanData(null);

            }
        });
    }

    public int getTodayPosition() {
        return mTodayPosition;
    }

    public List<SportPlanModel.DataBean> getSportPlanData() {
        return mDataBeen;

    }
}
